package wyh.service;

import org.apache.ibatis.session.SqlSession;
import wyh.mapper.EscortMapper;
import wyh.pojo.Escort;
import wyh.util.MyBatisUtils;

import java.util.Date;
import java.util.List;

public class EscortService {
    /**
     * @param like 模糊查询模糊查询
     * @return 返回一个患者对象列表b
     */
    public List<Escort> ServicegetByNameLike(String like) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            return escortMapper.MappergetByNameLike(like);
        }
    }

    /**
     * 需要移除 sqlSession.close(); 这行代码，因为 try-with-resources 语句会在 try 块结束时自动关闭 SqlSession
     */
    public List<Escort> ServicegetAll() {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            return escortMapper.MappergetAll();
        }
    }

    public Escort ServicegetById(int escortId) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            return escortMapper.MappergetById(escortId);
        }
    }

    public boolean ServicedeleteById(int escortId) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            int result = escortMapper.MapperdeleteById(escortId);
            return result > 0;
        }
    }

    public boolean Serviceupdate(Escort escort) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            int result = escortMapper.MapperupdateUserById(escort);
            return result > 0;
        }
    }

    public boolean Serviceadd(Escort escort) {
        try (SqlSession sqlSession = MyBatisUtils.getSession(true)) {
            EscortMapper escortMapper = sqlSession.getMapper(EscortMapper.class);
            int result = escortMapper.MapperaddUser(escort);
            return result > 0;
        }
    }
}